package com.telecom.cms.dao;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import com.telecom.cms.dto.CampSeg;
import com.telecom.cms.util.Constant;
import com.telecom.cms.util.CurrentTime;

public class CampSegDAO {
	Dao dao = new DaoImp();

	String db = (new Constant()).getPara("DATABASE");

	public int insert(CampSeg transientInstance) {
		int rs = 0;
		String sql = "select * from  " + db + ".Camp_Seg where Config_Id="
				+ transientInstance.getConfigId();
		try {
			ResultSet rs2 = dao.select(sql);
			if (rs2.next())
				sql = "update " + db + ".Camp_Seg set Camp_Id='"
						+ transientInstance.getCampId() + "',Show_Area_Id='"
						+ transientInstance.getShowAreaId() + "',Biz_Atom_Id='"
						+ transientInstance.getBizAtomId() + "',Creator_Name='"
						+ transientInstance.getCreatorName()
						+ "',AREA_SHOW_TYPE='"
						+ transientInstance.getAreaShowType()
						+ "',Show_Order='" + transientInstance.getShowOrder()
						+ "',Create_Time=timestamp '"
						+ CurrentTime.getCurrentTime() + "' where config_id="
						+ transientInstance.getConfigId();
			else
				sql = "insert into " + db + ".Camp_Seg values('"

				+ transientInstance.getConfigId() + "' , '"
						+ transientInstance.getCampId() + "' , '"
						+ transientInstance.getShowAreaId() + "' , '"
						+ transientInstance.getBizAtomId() + "' , '"
						+ transientInstance.getCreatorName() + "' , "
						+ "timestamp '" + CurrentTime.getCurrentTime()
						+ "', '" + transientInstance.getAreaShowType()
						+ "' , '" + transientInstance.getShowOrder() + " ')";
			rs = dao.insert(sql);
			if (rs2 != null) {

				rs2.close();
			}
		} catch (Exception re) {
			System.out.println("save failed:" + re);
		}
		finally
		{
			dao.closeconn();
		}
		return rs;

	}

	public void delete(CampSeg persistentInstance) {

		String sql = "delete from " + db + ".Camp_Seg where Config_Id = "
				+ persistentInstance.getConfigId();

		try {
			dao.delete(sql);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}
	
	public void deleteById(String campid,String areaid) {

		String sql = "delete from " + db + ".Camp_Seg where camp_Id = "
				+ campid+" and show_area_id="+areaid;

		try {
			dao.delete(sql);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	public CampSeg findById(String id) {

		String sql = "select * from " + db + ".Camp_Seg where Config_Id = "
				+ id;
		CampSeg sd = new CampSeg();
		try {
			ResultSet rs = dao.select(sql);
			if (rs.next()) {
				sd.setConfigId(rs.getInt(1));
				sd.setCampId(rs.getInt(2));
				sd.setShowAreaId(rs.getInt(3));
				sd.setBizAtomId(rs.getInt(4));
				sd.setCreatorName(rs.getString(5));
				sd.setCreateTime(rs.getString(6));
				sd.setAreaShowType(rs.getString(7));
				sd.setShowOrder(rs.getInt(8));

			}
			if (rs != null) {

				rs.close();
			}

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		finally
		{
			dao.closeconn();
		}
		return sd;

	}

	public List findAll() {

		String sql = "select * from " + db + ".Camp_Seg";

		List list = new ArrayList();
		try {
			ResultSet rs = dao.select(sql);
			while (rs.next()) {
				CampSeg sd = new CampSeg();
				sd.setConfigId(rs.getInt(1));
				sd.setCampId(rs.getInt(2));
				sd.setShowAreaId(rs.getInt(3));
				sd.setBizAtomId(rs.getInt(4));
				sd.setCreatorName(rs.getString(5));
				sd.setCreateTime(rs.getString(6));
				sd.setAreaShowType(rs.getString(7));
				sd.setShowOrder(rs.getInt(8));
				list.add(sd);
			}
			if (rs != null) {

				rs.close();
			}

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		finally
		{
			dao.closeconn();
		}
		return list;

	}

	public List findAll(String campid) {

		String sql = "select * from " + db + ".Camp_Seg where camp_id="
				+ campid;

		List list = new ArrayList();
		try {
			ResultSet rs = dao.select(sql);
			while (rs.next()) {
				CampSeg sd = new CampSeg();
				sd.setConfigId(rs.getInt(1));
				sd.setCampId(rs.getInt(2));
				sd.setShowAreaId(rs.getInt(3));
				sd.setBizAtomId(rs.getInt(4));
				sd.setCreatorName(rs.getString(5));
				sd.setCreateTime(rs.getString(6));
				sd.setAreaShowType(rs.getString(7));
				sd.setShowOrder(rs.getInt(8));
				list.add(sd);
			}
			if (rs != null) {

				rs.close();
			}

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		finally
		{
			dao.closeconn();
		}
		return list;

	}

	public List findAll(String campid, String showareaid) {

		String sql = "select * from " + db + ".Camp_Seg where camp_id="
				+ campid + " and show_area_id=" + showareaid;

		List list = new ArrayList();
		try {
			ResultSet rs = dao.select(sql);
			while (rs.next()) {
				CampSeg sd = new CampSeg();
				sd.setConfigId(rs.getInt(1));
				sd.setCampId(rs.getInt(2));
				sd.setShowAreaId(rs.getInt(3));
				sd.setBizAtomId(rs.getInt(4));
				sd.setCreatorName(rs.getString(5));
				sd.setCreateTime(rs.getString(6));
				sd.setAreaShowType(rs.getString(7));
				sd.setShowOrder(rs.getInt(8));
				list.add(sd);
			}
			if (rs != null) {

				rs.close();
			}

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		finally
		{
			dao.closeconn();
		}
		return list;

	}

}